package com.phantasy.scene.tile.gameAstar
{
	import com.phantasy.scene.sceneData.BinaryMath;
	import com.phantasy.scene.sceneData.MapDataVo;
	import com.phantasy.utils.ObjectUtils;
	
	public class MapGrid 
	{
		
		private var _startNode:Node;
		private var _endNode:Node;
		private var _numCols:int;
		private var _numRows:int;
		private var type:int;
		protected var _straightCost:Number = 1;
		protected var _diagCost:Number = 1.4142135623731;
		private var _nodes:Array;
		
		public function MapGrid(_arg1:MapDataVo)
		{
			this._nodes = (ObjectUtils.copy(_arg1.tiles) as Array);
		}
		
		public function calculateLinks():void
		{
			var _local1:Array;
			var _local3:int;
			var _local2:int;
			while (_local2 < this._nodes.length)
			{
				_local1 = this._nodes[_local2];
				_local3 = 0;
				while (_local3 < _local1.length)
				{
					if ((this._nodes[_local2][_local3] is Node))
					{
						this.initNodeLink(this._nodes[_local2][_local3]);
					};
					_local3++;
				};
				_local2++;
			};
		}
		
		public function initNodeLink(_arg1:Node):void
		{
			var _local7:int;
			var _local8:Node;
			var _local9:Number;
			if (_arg1.initLink)
			{
				return;
			};
			var _local2:int = (_arg1.x - 1);
			var _local3:int = (_arg1.x + 1);
			var _local4:int = (_arg1.y - 1);
			var _local5:int = (_arg1.y + 1);
			_arg1.links = [];
			var _local6:int = _local2;
			while (_local6 <= _local3)
			{
				_local7 = _local4;
				while (_local7 <= _local5)
				{
					_local8 = this.getNode(_local6, _local7);
					if (!(((_local8 == null)) || ((_local8 == _arg1))))
					{
						_local9 = this._straightCost;
						if (((!((_arg1.x == _local8.x))) && (!((_arg1.y == _local8.y)))))
						{
							_local9 = this._diagCost;
						};
						_arg1.links.push(new Link(_local8, _local9));
					};
					_local7++;
				};
				_local6++;
			};
			_arg1.initLink = true;
		}
		
		public function getNode(_arg1:int, _arg2:int):Node
		{
			var _local3:*;
			var _local4:Node;
			if ((((_arg1 >= 0)) && ((_arg1 < this._nodes.length))))
			{
				if ((((_arg2 >= 0)) && ((_arg2 < this._nodes[_arg1].length))))
				{
					_local3 = this._nodes[_arg1][_arg2];
					if ((_local3 is Node))
					{
						return ((_local3 as Node));
					};
					if (BinaryMath.isExist(int(_local3)) == true)
					{
						_local4 = new Node(_arg1, _arg2);
						this._nodes[_arg1][_arg2] = _local4;
						return (this._nodes[_arg1][_arg2]);
					};
					return (null);
				};
			};
			return (null);
		}
		
		public function setNodeWalkAble(_arg1:int, _arg2:int, _arg3:Boolean):void
		{
			var _local4:Node = this.getNode(_arg1, _arg2);
			if (_local4)
			{
				_local4.walkable = _arg3;
			};
		}
		
		public function getNodeWalkAble(_arg1:int, _arg2:int):Boolean
		{
			var _local3:Node = this.getNode(_arg1, _arg2);
			if (_local3)
			{
				return (_local3.walkable);
			};
			return (false);
		}
		
		public function setEndNode(_arg1:int, _arg2:int):void
		{
			this._endNode = this.getNode(_arg1, _arg2);
		}
		
		public function setStartNode(_arg1:int, _arg2:int):void
		{
			this._startNode = this.getNode(_arg1, _arg2);
		}
		
		public function get endNode():Node
		{
			return (this._endNode);
		}
		
		public function get numCols():int
		{
			return (this._numCols);
		}
		
		public function get numRows():int
		{
			return (this._numRows);
		}
		
		public function get startNode():Node
		{
			return (this._startNode);
		}
		
		
	}
}
